<?php
namespace App\Models\Trade\Demand;

use App\Models\Model;

class DemandCountVisitModel extends Model
{

    const TABLENAME = 'tw_trade_demand_count_visit';

    /**
     * 查询一条数据
     *
     * @param int $demandId            
     * @return \Illuminate\Support\Collection
     */
    public static function findByDemandId($demandId, array $fields = [])
    {
        $query = self::dbTable();
        if (count($fields)) {
            $query->select($fields);
        }
        
        return $query->where('demand_id', $demandId)->get();
    }

    /**
     * 统计自增
     *
     * @param string $field            
     * @param int $demandId            
     */
    protected static function inc($field, $demandId)
    {
        if (! $demandId) {
            return false;
        }
        $query = self::dbTable();
        
        $number = self::findByDemandId($demandId)->count();
        if ($number) {
            return $query->where('demand_id', $demandId)->increment($field);
        } else {
            return $query->insert([
                'demand_id' => $demandId,
                $field => 1
            ]);
        }
    }

    /**
     * 统计自减
     *
     * @param string $field            
     * @param int $demandId            
     */
    protected static function dec($field, $demandId)
    {
        if (! $demandId) {
            return false;
        }
        $query = self::dbTable();
        
        $item = self::findByDemandId($demandId)->shift();
        if ($item && $item[$field] > 0) {
            return $query->where('demand_id', $demandId)->decrement($field);
        }
        return false;
    }

    /**
     * 统计自减（批量修改）
     *
     * @param string $field            
     * @param int $demandIds            
     * @param int $userId            
     */
    protected static function decByDemandIds($field, $demandIds, $userId)
    {
        if (! $demandIds || ! count($demandIds) || ! $userId) {
            return false;
        }
        $where[] = ['dc.user_id', $userId];
        $where[] = ["dcv.$field", '>', 0];
        
        return self::dbTable('dcv')->where($where)
            ->join(DemandCollectionModel::TABLENAME . ' as dc', 'dc.demand_id', '=', 'dcv.demand_id')
            ->whereIn('dcv.demand_id', $demandIds)
            ->decrement("dcv.$field");
    }

    /**
     * pv +1
     *
     * @param int $demandId            
     */
    public static function incPv($demandId)
    {
        return self::inc('pv', $demandId);
    }

    /**
     * uv +1
     *
     * @param int $demandId            
     */
    public static function incUv($demandId)
    {
        return self::inc('uv', $demandId);
    }

    /**
     * collection +1
     *
     * @param int $demandId            
     */
    public static function incCollection($demandId)
    {
        return self::inc('collection', $demandId);
    }

    /**
     * collection -1
     *
     * @param int $demandId            
     */
    public static function decCollection($demandId)
    {
        return self::dec('collection', $demandId);
    }

    /**
     * collection -1
     *
     * @param int $demandIds            
     * @param int $userId            
     */
    public static function decCollectionByDemandIds($demandIds, $userId)
    {
        return self::decByDemandIds('collection', $demandIds, $userId);
    }

    /**
     * report +1
     *
     * @param int $demandId            
     */
    public static function incReport($demandId)
    {
        return self::inc('report', $demandId);
    }

    /**
     * dial +1
     *
     * @param int $demandId            
     */
    public static function incDial($demandId)
    {
        return self::inc('dial', $demandId);
    }
}
